<script setup lang="ts">
import { ref, watch, onUnmounted, onMounted } from 'vue';
import IconSwitch from '~icons/app/icon-switch.svg';

import { isClient, OScroller, OIcon } from '@opensig/opendesign';
import { useLocale } from '@/composables/useLocale.ts';
import { useScreen } from '@/composables/useScreen.ts';
import DocAnchorContent from '@/components/doc/DocAnchorContent.vue';


const switchAnchorRef = ref();
const { t } = useLocale();
const { size } = useScreen();
const docAnchorRef = ref();
// 锚点是否显示
const isAnchorVisible = ref(true);


const anchorData = ref([]);
const getAnchorData = (list) => {
  anchorData.value = list;
};


// -------------------- 屏幕小于1554px后，锚点折叠展开--------------------
const switchAnchor = () => {
  isAnchorVisible.value = !isAnchorVisible.value;
};
// -------------------- 监听屏幕小于1554px后锚点折叠--------------------
const adjustAnchor = () => {
  isAnchorVisible.value = size.width >= 1440;
  if (!anchorData.value.length && isClient) {
    docAnchorRef.value?.classList.add('hidden');
  } else if (isClient) {
    docAnchorRef.value?.classList.remove('hidden');
  }
};
const selector = '#app > .o-scroller > .o-scroller-container';
const anchorRef = ref();

const anchorChanged = (e: string) => {
  const wrapper = anchorRef.value?.querySelector('.o-scroller-container');
  if (!e) {
    wrapper.scrollTop = 0;
    return;
  }
  const activeAnchor = wrapper.querySelector(`a[href="${ e }"]`);
  if (!activeAnchor) {
    wrapper.scrollTop = 0;
    return;
  }
  const topHeight = activeAnchor.getBoundingClientRect().top + wrapper.scrollTop - wrapper.getBoundingClientRect().top;
  if (topHeight - 120 > 0) {
    wrapper.scrollTo({
      top: topHeight - 100,
      behavior: 'smooth',
    });
  } else {
    wrapper.scrollTop = 0;
  }
};

onMounted(() => {
  adjustAnchor();
  window.addEventListener('resize', adjustAnchor);
});

onUnmounted(() => {
  window.removeEventListener('resize', adjustAnchor);
});

watch(() => anchorData.value, () => {
  adjustAnchor();
}, {
  deep: true,
  immediate: true,
});
</script>

<template>
  <div class="doc-anchor" :class="isAnchorVisible ? '' : 'is-closed'" ref="docAnchorRef">
    <div class="anchor-wrapper" ref="anchorRef">
      <OScroller class="anchor-box" show-type="never" size="small" :disabled-x="true">
        <div class="anchor-tip">
          {{ t('docs.anchorTip') }}
          <div class="anchor-expand-opt" :class="isAnchorVisible ? '' : 'is-closed'" @click="switchAnchor"
               ref="switchAnchorRef">
            <OIcon>
              <IconSwitch></IconSwitch>
            </OIcon>
          </div>
        </div>
        <DocAnchorContent @getAnchorData="getAnchorData" @anchorChanged="anchorChanged"
                          :selector="selector"></DocAnchorContent>
      </OScroller>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.anchor-expand-opt {
  cursor: pointer;
  display: none;
  position: absolute;
  font-size: 24px;
  padding: 12px;
  right: 0;
  top: 0;
  z-index: 9;

  :deep(.o-icon) {
    font-size: 32px;

    path {
      fill: currentColor;
    }
  }
}

.anchor-expand-opt.is-closed {
  background-color: var(--o-color-fill2);
  border-radius: 0 100px 100px 0;
  box-shadow: var(--o-shadow-2);
  transform: scaleX(-1);
  position: fixed;
  right: 0;
  top: var(--layout-doc-anchor-top);
}

.anchor-box {
  width: 100%;
  max-height: calc(100vh - var(--layout-doc-anchor-top) - 24px - 256px - 96px);
  padding: 0;
}

.doc-anchor {
  width: 100%;
  height: 100%;
  position: sticky;
  top: var(--layout-doc-anchor-top);
  float: right;
  right: 0;


}

@include respond-to('<=laptop') {
  .anchor-box {
    background-color: var(--o-color-fill2);
    box-shadow: var(--o-shadow-2);
    padding: 16px 13px;
    border-radius: var(--o-radius-m);
  }
  .doc-anchor.is-closed {
    right: 0;
    z-index: 22;
    height: 0 !important;
    position: relative;
    left: calc(var(--layout-doc-anchor-width) + var(--grid--layout-padding));
    top: 52px;
  }
  .doc-anchor {
    margin-right: calc(0px - var(--grid--layout-padding));
    height: 0 !important;
  }
  .anchor-expand-opt {
    position: absolute;
    right: 0;
    top: 0;
    display: block;
  }
}

@include respond-to('>phone') {
  .doc-anchor {
    margin-right: calc(32px - var(--layout-doc-anchor-width) - var(--grid--layout-padding));
  }
  .anchor-wrapper {
    height: 100%;
  }
}


@include respond-to('phone') {
  .doc-anchor {
    display: none;
  }
}

.hidden {
  visibility: hidden;
}

.anchor-wrapper.is-closed {
  width: 100%;
  background-color: var(--o-color-info1);
  z-index: 666;
}

.anchor-tip {
  @include text2;
  margin-bottom: 16px;
  font-weight: 500;
  color: var(--o-color-info1);
  height: 24px;
}
</style>
